/*
 * Copyright 2012 Daniel Kurka
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.googlecode.mgwt.expenses.client.activities.el;

import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHTML;
import com.google.gwt.user.client.ui.Widget;
import com.googlecode.mgwt.dom.client.event.tap.TapEvent;
import com.googlecode.mgwt.ui.client.MGWT;
import com.googlecode.mgwt.ui.client.widget.CellList;
import com.googlecode.mgwt.ui.client.widget.ScrollPanel;
import com.googlecode.mgwt.ui.client.widget.base.ButtonBase;
import com.googlecode.mgwt.ui.client.widget.buttonbar.ButtonBar;
import com.googlecode.mgwt.ui.client.widget.celllist.CellSelectedEvent;

public class ExpenseListViewMobileGwtImpl extends Composite implements ExpenseListView {

	private static ExpenseListViewMobileGwtImplUiBinder uiBinder = GWT.create(ExpenseListViewMobileGwtImplUiBinder.class);

	interface ExpenseListViewMobileGwtImplUiBinder extends UiBinder<Widget, ExpenseListViewMobileGwtImpl> {
	}

	@UiField
	ButtonBase addButton;

	@UiField
	ButtonBase reloadButton;

	@UiField
	HTML title;

	@UiField
	HasHTML statusText;

	@UiField
	ScrollPanel scrollPanel;

	@UiField(provided = true)
	CellList<ExpenseListDTO> cellList;

	private Presenter presenter;

	@UiField
	ButtonBar buttonBar;

	public ExpenseListViewMobileGwtImpl() {

		cellList = new CellList<ExpenseListDTO>(new ExpenseListMobileCell());

		initWidget(uiBinder.createAndBindUi(this));
		if (MGWT.getOsDetection().isTablet()) {
			buttonBar.addStyleName("buttonBar-overlay");
		}

	}

	@Override
	public void setStatusText(String text) {
		statusText.setText(text);

	}

	@Override
	public void setPresenter(Presenter presenter) {
		this.presenter = presenter;

	}

	@UiHandler("addButton")
	public void onAddButtonSelected(TapEvent event) {
		if (presenter != null) {
			presenter.onAddButtonSelected();
		}
	}

	@UiHandler("reloadButton")
	public void onReloadButtonSelected(TapEvent event) {
		if (presenter != null) {
			presenter.onReloadButtonSelected();
		}
	}

	@UiHandler("cellList")
	public void onCellSelected(CellSelectedEvent event) {
		if (presenter != null) {
			presenter.onItemSelected(event.getIndex());
		}
	}

	@Override
	public void display(List<ExpenseListDTO> list) {
		cellList.render(list);
		scrollPanel.refresh();

	}

	@Override
	public void setTitleText(String title) {
		this.title.setText(title);

	}

	@Override
	public void selectItem(int index, boolean selected) {
		cellList.setSelectedIndex(index, selected);

	}

}
